package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.DaoSchoolChildMaterialDao;
import com.sc.nft.entity.DaoSchoolChild;
import com.sc.nft.entity.DaoSchoolChildMaterial;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.DaoSchoolChildMaterialNotPageAdminVO;
import com.sc.nft.entity.vo.DaoSchoolChildMaterialPageAdminVO;
import com.sc.nft.entity.vo.DaoSchoolChildMaterialPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoSchoolChildMaterialService;
import com.sc.nft.service.DaoSchoolChildService;
import com.sc.nft.service.DaoSchoolTopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 素材表与子集关联表(DaoSchoolChildMaterial)表服务实现类
 *
 * @author makejava
 * @since 2023-03-15 18:07:43
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DaoSchoolChildMaterialServiceImpl implements DaoSchoolChildMaterialService {

    private final DaoSchoolChildMaterialDao daoSchoolChildMaterialDao;

    private final DaoSchoolChildService daoSchoolChildService;

    private final DaoSchoolTopService daoSchoolTopService;

    /**
     * 获取关联的所有素材
     *
     * @param pageRequest
     * @return
     */
    @Override
    public Page<DaoSchoolChildMaterialPageAdminVO> pageAdmin(DaoSchoolChildMaterialPageAdminDTO pageRequest) {
        return daoSchoolChildMaterialDao.pageAdmin(pageRequest);
    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        // 根据关联id获取数据子集,根据自己获取父集 数据-1
        DaoSchoolChildMaterial daoSchoolChildMaterial = daoSchoolChildMaterialDao.getById(id);
        if (BeanUtil.isEmpty(daoSchoolChildMaterial)) {
            throw new GlobalRunTimeException("删除项不存在");
        }
        // 子集id
        Long schoolChildId = daoSchoolChildMaterial.getSchoolChildId();

        // 子集数据
        DaoSchoolChild daoSchoolChild = daoSchoolChildService.selectById(schoolChildId);
        // 子集文章总数-1
        daoSchoolChildService.totalSubOne(schoolChildId);

        // 父集文章总数-1
        daoSchoolTopService.totalSubOne(daoSchoolChild.getSecondTopId());
        return daoSchoolChildMaterialDao.deleteById(id);
    }

    @Override
    public Page<DaoSchoolChildMaterialNotPageAdminVO> selectByNotChildIdPageList(DaoSchoolMaterialNotChildIdPageListDTO dto) {
        return daoSchoolChildMaterialDao.selectByNotChildIdPageList(dto);
    }

    /**
     * 素材关联新增
     *
     * @param info
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdmin(List<DaoSchoolMaterialChildSaveAdminDTO> info) {
        info.forEach(daoSchoolArticleChildSaveAdminDTO -> {
            // 根据关联id获取数据子集,根据自己获取父集 数据-1
            DaoSchoolChild daoSchoolArticleChild = daoSchoolChildService.selectById(daoSchoolArticleChildSaveAdminDTO.getSchoolChildId());
            if (daoSchoolArticleChild.getType().equals(0)) {
                throw new GlobalRunTimeException("工具分类只允许使用工具子集");
            }
            if (BeanUtil.isEmpty(daoSchoolArticleChild)) {
                throw new GlobalRunTimeException("子集不存在");
            }
            // 父集ID
            Long schoolChildId = daoSchoolArticleChild.getSecondTopId();

            // 子集文章总数+1
            daoSchoolChildService.totalAddOne(daoSchoolArticleChildSaveAdminDTO.getSchoolChildId());

            // 父集文章总数+1
            daoSchoolTopService.totalAddOne(schoolChildId);
        });
        return daoSchoolChildMaterialDao.saveAdmin(info);
    }

    /**
     * 获取子集下的素材
     *
     * @param dto
     * @return
     */
    @Override
    public Page<DaoSchoolChildMaterialPageVO> getChildMaterialList(DaoSchoolHomeArticleListDTO dto) {
        return daoSchoolChildMaterialDao.getChildMaterialList(dto);
    }

    /**
     * 修改排序
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateSortById(DaoSchoolChildMaterialUpdateSortAdminDTO dto) {

        return daoSchoolChildMaterialDao.updateSortById(dto);
    }

}
